package eteam.aps.service.impl;

import eteam.aps.common.helper.JsonPage;
import eteam.aps.mapper.QueryInfoMapper;
import eteam.aps.mapper.WorksheetMapper;
import eteam.aps.model.entity.DataDictionaryCategory;
import eteam.aps.model.entity.Worksheet;
import eteam.aps.model.vo.WorksheetVo;
import eteam.aps.service.WorksheetService;
import eteam.aps.systeminterface.model.vo.QueryInfoVo;
import eteam.aps.systeminterface.model.vo.Worksheets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 单据流水号相关业务接口
 *
 * @author WMT
 * @Date 2023/2/17 14:12
 */
@Service
public class WorksheetServiceImpl implements WorksheetService {

    @Autowired
    private WorksheetMapper worksheetMapper;

    @Autowired
    private QueryInfoMapper queryInfoMapper;

    @Transactional
    @Override
    public int insert(Worksheet worksheet) {
        return worksheetMapper.insert(worksheet);
    }

    @Transactional
    @Override
    public int delete(String fId) {
        return worksheetMapper.deleteOne(fId);
    }

    @Transactional
    @Override
    public int update(Worksheet worksheet) {
        return worksheetMapper.updateOne(worksheet);
    }

    @Override
    public JsonPage<WorksheetVo> selectAll(Map<String, Object> map, int page, int pageSize) {
        int totalCount = worksheetMapper.selectCountAll(map);
        List<WorksheetVo> list = null;
        if (totalCount > 0) {
            map.put("offset", (page - 1) * pageSize);
            map.put("limit", pageSize);
            list = worksheetMapper.selectAll(map);
        }
        return JsonPage.builder(page, pageSize, totalCount, list);
    }

    @Override
    public List<QueryInfoVo> selectTypeByOrder(int order) {
        List<QueryInfoVo> queryInfoVos = null;
        switch (order) {
            case Worksheet.FWORKSHEET_TYPE_OTHER:
                queryInfoVos = queryInfoMapper.dictList(DataDictionaryCategory.FDDCNAME_OTHER_ORDER);
                break;
            case Worksheet.FWORKSHEET_TYPE_BASIC:
                queryInfoVos = queryInfoMapper.dictList(DataDictionaryCategory.FWORKSHEET_TYPE_BASIC);
                break;
            case Worksheet.FWORKSHEET_TYPE_SALES:
                queryInfoVos = queryInfoMapper.dictList(DataDictionaryCategory.FWORKSHEET_TYPE_SALES);
                break;
            case Worksheet.FWORKSHEET_TYPE_PURCHAS:
                queryInfoVos = queryInfoMapper.dictList(DataDictionaryCategory.FWORKSHEET_TYPE_PURCHAS);
                break;
            case Worksheet.FWORKSHEET_TYPE_INVENTORY:
                queryInfoVos = queryInfoMapper.dictList(DataDictionaryCategory.FWORKSHEET_TYPE_INVENTORY);
                break;
            case Worksheet.FWORKSHEET_TYPE_PRODUCTION:
                queryInfoVos = queryInfoMapper.dictList(DataDictionaryCategory.FWORKSHEET_TYPE_PRODUCTION);
                break;
            case Worksheet.FWORKSHEET_TYPE_ACCOUNTS_RECEIVABLE:
                queryInfoVos = queryInfoMapper.dictList(DataDictionaryCategory.FWORKSHEET_TYPE_ACCOUNTS_RECEIVABLE);
                break;
            case Worksheet.FWORKSHEET_TYPE_ACCOUNT_PAYABLE:
                queryInfoVos = queryInfoMapper.dictList(DataDictionaryCategory.FWORKSHEET_TYPE_ACCOUNT_PAYABLE);
                break;
        }
        return queryInfoVos;
    }

    @Override
    public WorksheetVo selectDetails(String fId) {
        return worksheetMapper.selectDetails(fId);
    }

    @Override
    public Worksheets selectSheetPrefix(String sheetCode) {
        return worksheetMapper.selectSheetPrefix(sheetCode);
    }

    @Override
    public List<QueryInfoVo> selectDdByWorksheetType() {
        return worksheetMapper.selectDdByWorksheetType();
    }

    @Override
    public int selectCountWorksheetCode(Map<String, Object> map) {
        return worksheetMapper.selectCountWorksheetCode(map);
    }

    @Override
    public int selectCountWorksheetName(Map<String, Object> map) {
        return worksheetMapper.selectCountWorksheetName(map);
    }

    @Override
    public QueryInfoVo selectCodeByWorksheetName(String name) {
        return worksheetMapper.selectCodeByWorksheetName(name);
    }

    @Override
    public QueryInfoVo selectNameByCode(String code) {
        return worksheetMapper.selectNameByCode(code);
    }
}
